package com.example.web;

import com.example.pojo.Book;
import com.example.pojo.Page;
import com.example.service.BookService;
import com.example.service.impl.BookServiceImpl;
import com.example.utils.WebUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "BookServlet", value = "/manager/BookServlet")
public class BookServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 0);
        pageNo++;
        //1.获取请求参数，封装成Book对象
        Book book = WebUtils.copyParamToBean(request.getParameterMap(),new Book());
        //2.调用bookService.addBook()保存图书
        bookService.addBook(book);
        //3.跳到图书列表页面
        //不行，因为request请求没断，用户使用f5会重复上次请求，即不停添加
        //request.getRequestDispatcher("/manager/BookServlet?action=list").forward(request, response);
        response.sendRedirect(request.getContextPath()+"/manager/BookServlet?action=page&pageNo="+pageNo);
    }

    /**
     * 处理分页功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数pageNo,pageSize
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用bookService.page(pageNo,pageSize);Page对象
        Page<Book> page = bookService.page(pageNo,pageSize);
        page.setUrl("manager/BookServlet?action=page");
        //3.保存Page对象到Request域中
        request.setAttribute("page", page);
        //4.请求转发到pages/manager/book_manger.jsp页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求的参数id，图书编号
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        //2.调用bookService.deleteById(),删除图书
        bookService.deleteBookById(id);
        //3.重定向回图书列表
        response.sendRedirect(request.getContextPath()+"/manager/BookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        bookService.updateBook(book);
        response.sendRedirect(request.getContextPath()+"/manager/BookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }

    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数图书编号
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        //2.调用bookService.queryBookById查询图书
        Book book = bookService.queryBookById(id);
        //3.保存图书到Request域中
        request.setAttribute("book", book);
        //4.请求转发到pages/manager/book_edit.jsp
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request, response);
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.通过BookService查询全部图书
        List<Book> books = bookService.queryBooks();
        //2.把全部图书保存到Request域中
        request.setAttribute("books",books);
        //3.请求转发到/pages/manager/book_manager.jsp页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }

}
